/*
 *  Copyright 2020-2025 the original author or authors.
 *  You cannot use this file unless authorized by the author.
 */

package org.ipig.model;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.ipig.constants.SymbolCnst;
import org.ipig.entity.GenericModelEntity;

import java.io.Serializable;

/**
 * 模型记录
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: ModelRecord.java 15 2020-04-21 12:04:17Z ts <comchnts@163.com> $$
 * @since 1.0
 */
@ToString
@Data
public class ModelRecord<M1, M2> implements Serializable {
    public transient static final String TABLE = "table";//对于OGG过来的数据格式为数据库名.表名
    public transient static final String OP_TYPE = "op_type";//	操作类型【I,D,U】
    public transient static final String OP_TS = "op_ts";//	事务操作时间戳，同一业务主键，事务操作时间戳一定唯一
    public transient static final String CURRENT_TS = "current_ts";//	当前时间戳
    public transient static final String POS = "pos";//	记录全局唯一ID
    public transient static final String PRIMARY_KEYS = "primary_keys";//	主键key数组，有序
    public transient static final String BEFORE = "before";//	操作前业务数据
    public transient static final String AFTER = "after";//	操作后业务数据

    //通常通过ogg采集的消息才有值,且为数据库名
    private String schema;
    //通过ogg采集的消息为数据库的表名
    private String code;
    //操作类型，参考DbmsOperators.OpType
    private String opType;
    //操作时间戳，如2019-12-17T13:58:40.859000 或 20191217135840859000
    private String opTs;
    //当前时间戳,如2019-12-17 05:58:38.697077 或 20191217055838697077
    private String currentTs;
    //记录全局唯一ID
    private String pos;
    //有序的主键key数组
    private String[] primaryKeys;
    //操作后业务数据
    private M1 afterData;
    //操作前业务数据
    private M2 beforeData;

    /**
     * 构建模型JSON对象
     *
     * @return JSONObject
     */
    public JSONObject buildModelJSON() {
        JSONObject model = new JSONObject();
        model.put(POS, this.getPos());
        model.put(OP_TS, this.getOpTs());
        model.put(CURRENT_TS, this.getCurrentTs());
        model.put(OP_TYPE, this.getOpType());
        model.put(PRIMARY_KEYS, this.getPrimaryKeys());
        model.put(BEFORE, this.getBeforeData());
        model.put(AFTER, this.getAfterData());
        if (StringUtils.isNotBlank(this.getSchema())) {
            model.put(TABLE, this.getSchema() + SymbolCnst.DOT + this.getCode());
        } else {
            model.put(TABLE, this.getCode());
        }
        return model;
    }

    /**
     * 构建模型实体对象
     *
     * @return JSONObject
     */
    public GenericModelEntity buildModelEntity() {
        GenericModelEntity entity = new GenericModelEntity();
        entity.setPos(this.getPos());
        entity.setOp_type(this.getOpTs());
        entity.setCurrent_ts(this.getCurrentTs());
        entity.setPrimary_keys(this.getPrimaryKeys());
        entity.setAfter(this.getAfterData());
        entity.setBefore(this.getBeforeData());
        if (StringUtils.isNotBlank(this.getSchema())) {
            entity.setTable(this.getSchema() + SymbolCnst.DOT + this.getCode());
        } else {
            entity.setTable(this.getCode());
        }
        return entity;
    }
}

